﻿#include "unrealircd.h"


CMD_FUNC(cmd_delete_user);
HTTPREQUESTSUCCESSCALLBACK(gitlabDeleteUserSuccessCallfunc);
HTTPREQUESTFAILCALLBACK(gitlabDeleteUserFailCallfunc);

ModuleHeader MOD_HEADER
= {
	"third/deleteUser",
	"1.1",
	"This module will send phone message ",
	"waitch",
	"unrealircd-5",  
};

MOD_INIT()
{
	CommandAdd(modinfo->handle, "DELETEUSER", cmd_delete_user, 4, CMD_USER);
	

	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}

/**
 * 删除用户的命令
 * parv[1]: 验证码
 * parv[2]: 验证方式
 * parv[3]: 发送的目标（手机号或邮箱）
 * parv[4]: 删除用户id 可以为null，这样默认为用户本身，如果指定其他用户则必须为管理员用户才能指定
 */ 

CMD_FUNC(cmd_delete_user){
	if (parc == 4&&BadPtr(parv[3])) {
		sendnumeric(client, PARAMERROR, "DELETEUSER","parv[3]");
		return;
	}
	else if(parc==5&&BadPtr(parv[4])){
		sendnumeric(client, PARAMERROR, "DELETEUSER","parv[4]");
		return;
	}
	else if(parc<4){
		sendnumeric(client, ERR_NEEDMOREPARAMS, "DELETEUSER");
		return;
	}
	if(!is_digist_string(parv[2],strlen(parv[2]))){
		sendnumeric(client, PARAMERROR, "DELETEUSER","parv[2]");
		return;
	}
	if(parc==5&&!is_digist_string(parv[4],strlen(parv[4]))){
		sendnumeric(client, PARAMERROR, "DELETEUSER","parv[4]");
		return;
	}
	int data_len = 0;
	D_User *d_user = queryImUserById(client->user_id, &data_len);
	int sendtype=atoi(parv[2]);
	char *sendValue=NULL;
	switch(sendtype){
		case(SMS_MODE_PHONE)://验证短信发送的验证码是否正确
			if(strcmp(d_user->tel,parv[3])){
				sendValue = NULL;
			}
			else{
				safe_strdup(sendValue,d_user->tel);
			}

			break;
		case(SMS_MODE_EMAIL)://验证邮箱发送的验证码是否正确
			{
				int temp_user_email_len =0;
				D_im_user_email * temp_user_email = queryImUserEmailByIdAndEmail(client->user_id,parv[3],&temp_user_email_len);
				if(temp_user_email_len != 1){
					sendValue =NULL;
				}
				else{
					safe_strdup(sendValue,parv[3]);
				}
				db_D_UserEmailFree(temp_user_email,temp_user_email_len);
				break;
			}
		default:
			sendnumeric(client, PARAMERROR, "DELETEUSER","parv[2]");
			return;
	}
	if(!sendValue){
		sendnumeric(client, PARAMERROR, "DELETEUSER","parv[2]");
		return;
	}
	//判断验证码是否有效
	if(!D_SMSVerificationCode(sendValue,parv[1],sendtype,SMS_STATUS_DELETE)){
		sendnumeric(client, SMSVALIDATIONFAILED);
		return;
	}

	int delete_id=0;
	if(parc==4){
		delete_id=-1;
		int parv3=atoi(parv[3]);
		if(parv3==*(d_user->id)){
			delete_id=parv3;
		}
		else if(1){//判断是否为管理员管理员可以删除其他人员的账号,//待写
			delete_id=parv3;
		}
	}
	if(delete_id==-1){
		sendnumeric(client, DELETEUSERFAILE, "Permission denied");
		return;
	}
	int deleteStatus=0;
	char delete_error_message[200]="";
	D_User *delete_user = deleteImUserById(*(d_user->id),&deleteStatus,delete_error_message);
	db_D_UserFree(d_user,deleteStatus);
	if(deleteStatus==-1){
		sendnumeric(client, DELETEUSERFAILE, "delete user faile");
		return;
	}
	ircd_log(LOG_ERROR,"database delete user:%d success",*(delete_user->id));
	sendnumeric(client, DELETEUSERSUCCESS, delete_user->user_name);
	exit_client(client,NULL,"User has been deleted");
	// char url[50];
	// sprintf(url,"/users/%d",*(delete_user->git_id));
	// HttpRequestData * data = malloc(sizeof(HttpRequestData));
	// data->url = get_gitlib_api_url(url);
	// ircd_log(LOG_ERROR,"gitlib delete user by id:%d,url:%s",*(delete_user->git_id),data->url);
	// data->data =NULL;
	// data->userPoint = delete_user;
    // data->client=client;
	// data->requestType = 3;
	// data->head = get_gitlib_head(&(data->head_num));
	// data->successfinishCallFun = gitlabDeleteUserSuccessCallfunc;
	// data->failefinishCallFun = gitlabDeleteUserFailCallfunc;
	// add_httpRequestReady(data);
}

HTTPREQUESTSUCCESSCALLBACK(gitlabDeleteUserSuccessCallfunc){
	D_User *delete_user=(D_User *)data;
	ircd_log(LOG_ERROR,"gitlabDeleteUserSuccessCallfunc httpCode:%ld",httpCode);
	switch (httpCode){
		case 204:
			//成功删除用户
			ircd_log(LOG_ERROR,"gitlab delete user:%d success",*(delete_user->id));
			db_D_UserFree(delete_user,1);
			break;
		case 404:
			//未找到删除用户的数据
			gitlabDeleteUserFailCallfunc(requestData,client,data);
			break;
		case 409:
			//用户不能被删除
			gitlabDeleteUserFailCallfunc(requestData,client,data);
			break;
		default:
			//其他未知原因不能被删除
			gitlabDeleteUserFailCallfunc(requestData,client,data);
			break;
	}
}


HTTPREQUESTFAILCALLBACK(gitlabDeleteUserFailCallfunc){
	D_User *delete_user=(D_User *)data;
	ircd_log(LOG_ERROR,"gitlab delete user:%d faile",*(delete_user->id));
	db_D_UserFree(delete_user,1);
}